package com.bingxue.edu.management.system.controller;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.common.model.SortQuery;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.auth.service.SocialAccountService;
import com.bingxue.edu.management.system.model.entity.SocialAccount;
import com.bingxue.edu.management.system.model.query.SocialAccountQuery;
import com.bingxue.edu.management.system.model.req.SocialAccountReq;
import com.bingxue.edu.management.system.model.resp.SocialAccountDetailResp;
import com.bingxue.edu.management.system.model.resp.SocialAccountResp;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 社交账户接口
 *
 * @author panbo
 * @since 2024/07/24 18:12
 */
@Tag(name = "社交账户接口")
@RestController
@RequestMapping("/system/socialAccount")
@RequiredArgsConstructor
public class SocialAccountController {

    private final SocialAccountService socialAccountService;

    @Operation(summary = "查询列表")
    @ApiResource("system:org:manage")
    @GetMapping("/list")
    public Response<List<SocialAccountResp>> list(SocialAccountQuery query, SortQuery sortQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, sortQuery);
        List<SocialAccountResp> list = socialAccountService.listAs(queryWrapper, SocialAccountResp.class);
        return Response.success(list);
    }

    @Operation(summary = "分页查询列表")
    @ApiResource("system:org:manage")
    @GetMapping("/page")
    public Response<Pagination<SocialAccountResp>> page(SocialAccountQuery query, @Validated PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, pageQuery);
        Page<SocialAccountResp> page = socialAccountService.pageAs(pageQuery.toPage(), queryWrapper, SocialAccountResp.class);
        return Response.success(Pagination.of(page));
    }

    @Operation(summary = "查询详情")
    @ApiResource("system:org:manage")
    @GetMapping("/detail/{id}")
    public Response<SocialAccountDetailResp> detail(@PathVariable String id) {
        SocialAccountDetailResp detail = socialAccountService.getMapper().selectOneWithRelationsByIdAs(id, SocialAccountDetailResp.class);
        return Response.success(detail);
    }

    @Operation(summary="创建数据")
    @ApiResource("system:org:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody @Validated SocialAccountReq req) {
        SocialAccount socialAccount = socialAccountService.copy(req);
        socialAccountService.save(socialAccount);
        return Response.success();
    }

    @Operation(summary="更新数据")
    @ApiResource("system:org:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody @Validated SocialAccountReq req, @PathVariable String id) {
        SocialAccount socialAccount = socialAccountService.copyById(req, id);
        socialAccountService.updateById(socialAccount);
        return Response.success();
    }

    @Operation(summary="删除数据")
    @ApiResource("system:org:manage")
    @PostMapping("/delete/{ids}")
    public Response<Void> delete(@PathVariable List<String> ids) {
        socialAccountService.removeByIds(ids);
        return Response.success();
    }

}
